

import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageInputStreamImpl;
import javax.swing.*;
import java.net.*;


public class httpConn {
    
    private final String CrLf = "\r\n";
    HttpURLConnection conn = null;
    DataOutputStream os = null;
    DataInputStream is = null;
    
    public void init(){
        ;
    }
    
    public void SendImage(Image image,String ServerUrl){
        try{
            
            BufferedImage imBuff = new BufferedImage(image.getWidth(null),
                    image.getHeight(null),
                    BufferedImage.TYPE_INT_RGB);
            Graphics bg = imBuff.getGraphics();
            bg.drawImage(image, 0, 0, null);
            bg.dispose();
            
            //pegar tamanho da imagem
            long imageSize = imBuff.getData().getDataBuffer().getSize();
            System.out.println("Tamanho:" + imageSize);
            System.out.println("url:" + ServerUrl);
            try{
                URL url = new URL(ServerUrl);
                conn = (HttpURLConnection)url.openConnection();
                conn.setRequestMethod("POST");
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setUseCaches(false);
               
                conn.setRequestProperty("Content-Type", "multipart/form-data, boundary=CASNAV");
                
                String command = "--CASNAV" + CrLf
                        + "Content-Disposition: form-data; name=\"datafile\"; filename=\"image.jpg\"" + CrLf
                        + "Content-Type: image/jpeg" + CrLf
                        + "Content-Transfer-Encoding: binary" + CrLf
                        + CrLf;
                
                String trail = CrLf + "--CASNAV--" + CrLf;
                
                conn.setRequestProperty("Content-Length",""+imageSize+command.length()+trail.length());
                
                System.out.println("open os");
                os = new DataOutputStream(conn.getOutputStream());
                
                System.out.println(command);
                os.write(command.getBytes());
                
                // SEND THE IMAGE
                ImageIO.write(imBuff,"jpg",os);
                System.out.println("Enviando imagem");
                
                System.out.println(trail);
                os.write(trail.getBytes());
                
                System.out.println("Bytes enviados: "+os.size());
                os.flush();
                
                System.out.println("Abrindo IS");
                is =  new DataInputStream(conn.getInputStream());
                
                char buff = 512;
                int len;
                byte []data = new byte[buff];
                do{
                    System.out.println("LENDO");
                    len = is.read(data);
                    
                    if(len > 0){
                        System.out.println(new String(data, 0, len));
                    }
                }while(len>0);
                
                System.out.println("DONE");
            } catch (Exception e2) {
                System.out.println(e2.getMessage());
            }
        }catch(Exception e){
            e.printStackTrace();   
        }finally{
            System.out.println("Close connection");
            try{
                os.close();
            }catch(Exception e){}
            try{
                is.close();
            }catch(Exception e){}
            try{
                conn.disconnect();
            }catch(Exception e){}
        }
    }
}